package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ProjectsMapper;
import com.ruoyi.system.mapper.ScreenMapper;
import com.ruoyi.system.service.ScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ScreenServiceImpl implements ScreenService {
    @Autowired
    private ScreenMapper screenMapper;
    @Autowired
    private ProjectsMapper projectsMapper;
    //实际投资与计划投资
    @Override
    public List<Invest> selectInvest(Invest invest) {
        List list = new ArrayList();
        for (int i = 1; i <= 12; i++) {
            Invest invest1 = new Invest();
            //拼接日期
            String time="";
            if (i<10){
                time=invest.getMouYear()+"-0"+i;

            }else {
                time=invest.getMouYear()+"-"+i;
            }
            //判断pid是否为空
            invest.setMouMouth(String.valueOf(i));
            if (null!=invest.getProId()){
                invest1 = screenMapper.selectInvestById(invest);
            }else {
                //根据部门Id查询
                int deptId = projectsMapper.selectDept(invest.getUserId());
                //是否为政府部门
                //判断是否为admin
                if (deptId!=-1) {
                    if (0 == projectsMapper.selectDeptId(deptId)) {
                        invest.setDeptId(-1);
                    } else {
                        invest.setDeptId(deptId);
                    }
                }
                invest1 = screenMapper.selectInvest(invest);
            }
            //添加空的对象及日期
            if (null==invest1){
                Invest invest2 = new Invest();
                invest2.setMouYearMouth(time);
                list.add(invest2);
            }else {
                invest1.setMouYearMouth(time);
                list.add(invest1);
            }
        }
        return list;
    }

    @Override
    public Map<String, Object> selectIndustry(String mouYear) {
        List<String> type=screenMapper.selectType();
        Map<String, Object> map = new HashMap<>();
        HashMap<Object, Object> hashMap = new HashMap<>();
        for (int i = 0; i < type.size(); i++) {
            hashMap.put("typName",type.get(i));
            hashMap.put("mouYear",mouYear);
            Double industry=screenMapper.selectIndustry(hashMap);
            map.put(type.get(i),industry);
        }
        return map;
    }

    @Override
    public Map<String, Object> selectArea(String disType,String mouYear,String userId) {
        List<String> area=screenMapper.selectArea(disType);
        Map<String, Object> map = new HashMap<>();
        HashMap<Object, Object> hashMap = new HashMap<>();
        for (int i = 0; i < area.size(); i++) {
            hashMap.put("disName",area.get(i));
            hashMap.put("mouYear",mouYear);
            //根据部门Id查询
            int deptId = projectsMapper.selectDept(userId);
            //是否为政府部门
            //判断是否为admin
            if (deptId!=-1) {
                if (0 == projectsMapper.selectDeptId(deptId)) {
                    hashMap.put("deptId",-1);
                } else {
                    hashMap.put("deptId",deptId);
                }
            }
            Double investment=screenMapper.selectInvestment(hashMap);
            map.put(area.get(i),investment);
        }
        return map;
    }
    /** @Description:  查看所有问题
    * @Param: []
    * @returns: java.util.List<com.ruoyi.system.domain.ProjectIssue>
    * @Author: Administrator
    * @Date: 2021/1/26 9:24
    **/

    @Override
    public List<ProjectIssue> queryIssue(ProjectIssue projectIssue) {
        return screenMapper.queryIssue(projectIssue);
    }

    @Override
    public List<Coordinate> getCoordinate(Projects projects) {
        //根据部门Id查询
        int deptId = projectsMapper.selectDept(projects.getUserId());
        //判断是否为admin
        if (deptId!=-1) {
            if (0 == projectsMapper.selectDeptId(deptId)) {
                projects.setDeptId(-1);
            } else {
                projects.setDeptId(deptId);
            }
        }
        List<Coordinate> list=screenMapper.getCoordinate(projects);
        return list;
    }
    //左边数据
    public Map<String, Object> queryList(String userId) {
        //部门
        int dept = projectsMapper.selectDept(userId);
        if (dept!=-1) {
            if (0 == projectsMapper.selectDeptId(dept)) {
                dept=-1;
            }
        }
        Map<String, Object> exl=new HashMap<>();
        exl.put("sum", projectsMapper.sum(dept));
        exl.put("plan1", projectsMapper.selectplan(0 ,dept));
        exl.put("plan2", projectsMapper.selectplan(1,dept));
        List<Nature> natures = projectsMapper.queryNature();
        List list= new ArrayList<>();
        for (int i = 0; i < natures.size(); i++) {
            HashMap map = new HashMap();
            map.put("sum" , projectsMapper.querycountBynatName(natures.get(i).getNatName(),dept));
            map.put("name" ,natures.get(i).getNatName());
            list.add(map);
        }
        exl.put("natures",list);
        return exl;
    }

}
